<template>
  <div>
    <p>{{msg}}</p>
    <div v-if="flag">
      <span>转入账号:</span><input type="text" maxlength="16" v-model="formData.otherCardNo"><br>
      <span>转账金额:</span><input type="text" v-model="formData.money"><br>
      <input type="button" value="转账" @click="transfer()">
    </div>
  </div>
</template>

<script>
import instance from "../../utils/request";

export default {
  name: "TransferAccounts",//转账
  data(){
    return {
      msg:'当前操作:转账。请输入转入账号和金额后点“转账“按钮',
      formData:{},
      flag:true
    }
  },
  methods:{
    transfer(){
      let m = this.formData.money;
      let a = this.formData.otherCardNo;
      let y = String(m).indexOf(".") + 1;//获取小数点的位置
      let count = String(m).length - y;//获取小数点后的个数
      if (Object.keys(this.formData).length <2){//非空验证
        alert('账号和金额均为必填项');
      }else if (isNaN(a) || !(a.length==16)){
        alert('账号只能是16位数字');
      }
      else if (isNaN(m) || !(count<=2)){
        alert('金额必须是数字且最多只能有两位小数');
      }
      else {
        instance.post('/account/transfer',this.formData).then(res=>{
          if (res.data.indexOf('成功')>0){//成功
            this.msg = res.data;
            this.flag = false;
            setTimeout(()=>{//延时函数
              this.msg = '当前操作:转账。请输入转入账号和金额后点“转账“按钮';
              this.flag = true;
              this.formData.money = '';//清空表单
              this.formData.otherCardNo = '';
            },5000);
          }else {
            alert(res.data);//弹出提示信息
          }
        });
      }
    }
  },
  created() {
    this.formData.loginCardNo = this.$route.params.cardno;//路由接收值
    if (typeof this.formData.loginCardNo === 'undefined'){//未登录
      this.$router.push({name:'Login'});
    }
  }
}
</script>

<style scoped>
div{
  margin-top: 50px;
  /*黑体*/
  font-family:SimHei;
}
p{
  font-weight: bold;
}
input{
  /*li上下间距*/
  margin-top: 5px;
  margin-left: 10px;
}
</style>
